package com.ganjiangps.huochetou.service.impl;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ganjiangps.huochetou.mapper.PersonalManagementMapper;
import com.ganjiangps.huochetou.mapper.PersonalManagementNoQuoteMapper;
import com.ganjiangps.huochetou.mapper.PersonalManagementUserMapper;
import com.ganjiangps.huochetou.model.PersonalManagement;
import com.ganjiangps.huochetou.model.PersonalManagementNoQuote;
import com.ganjiangps.huochetou.model.PersonalManagementUser;
import com.ganjiangps.huochetou.service.PersonalManagementService;

/**
 * 人员名单
 * 
 * @author xiaoy
 *
 */
@Service
public class PersonalManagementServiceImpl extends ServiceImpl<PersonalManagementMapper, PersonalManagement>
		implements PersonalManagementService {
	@Resource
	private PersonalManagementNoQuoteMapper personalManagementNoQuoteMapper;
	@Resource
	private PersonalManagementUserMapper personalManagementUserMapper;

	// @Resource
	// private TenderRedpacketRulesService tenderRedpacketRulesService;
	// @Resource
	// private GroupRedrulesService groupRedrulesService;
	// @Resource
	// private RechargeSetRelevancyService rechargeSetRelevancyService;
	// @Resource
	// private SmsRemoveService smsRemoveService;
	// @Resource
	// UserAgencySettingSetRelevancyService
	// userAgencySettingSetRelevancyService;
	//
	@Override
	public List<PersonalManagement> selectByMap(Map<String, Object> columnMap) {
		List<PersonalManagement> list = super.selectByMap(columnMap);
		for (PersonalManagement p : list) {
			String pmno = p.getPmno();

			p.setQuote(0);

			if (p.getPmtype().equals(1)) {
				int size = personalManagementNoQuoteMapper
						.selectCount(new EntityWrapper<PersonalManagementNoQuote>().eq("parentNo", pmno));
				p.setSonsize(size);
				int num = personalManagementUserMapper.listUsers(pmno).size();
				p.setNum(num);
			} else if (p.getPmtype().equals(2)) {
				int num = personalManagementUserMapper
						.selectList(new EntityWrapper<PersonalManagementUser>().eq("pmNo", pmno)).size();
				p.setNum(num);
			}
		}
		return list;
	}

	@Override
	public List<PersonalManagement> selectList(Wrapper<PersonalManagement> wrapper) {
		// TODO Auto-generated method stub
		List<PersonalManagement> list = super.selectList(wrapper);
		for (PersonalManagement p : list) {
			String pmno = p.getPmno();

			p.setQuote(0);

			if (p.getPmtype().equals(1)) {
				int size = personalManagementNoQuoteMapper
						.selectCount(new EntityWrapper<PersonalManagementNoQuote>().eq("parentNo", pmno));
				p.setSonsize(size);
				int num = personalManagementUserMapper.listUsers(pmno).size();
				p.setNum(num);
			} else if (p.getPmtype().equals(2)) {
				int num = personalManagementUserMapper
						.selectList(new EntityWrapper<PersonalManagementUser>().eq("pmNo", pmno)).size();
				p.setNum(num);
			}
		}
		return list;

	}

	//
	@Override
	@Transactional
	public boolean deleteById(Serializable id) {
		/*
		 * 名单类型为小名单时，删除小名单下的所有用户
		 */
		PersonalManagement p = baseMapper.selectById(id);
		if (p.getPmtype().equals(2)) {
			personalManagementUserMapper.delete(new EntityWrapper<PersonalManagementUser>().eq("pmNo", p.getPmno()));
		}
		return super.deleteById(id);
	}

}
